<?php
require_once("mysqlUser/mysql.php");

class mission
{
	public static $table = "missions";
	
	public static function addMission($uid,$mid,$isgot=0)
	{
		$time_now = mission::getCurrentTime();
		$command =  "INSERT INTO ".mission::$table." VALUES (null,$uid,$mid,$isgot,'$time_now',0,0)"; 
		mysql::run($command);
	}
	
	public static function delMission($uid,$mid)
	{
		$command = "delete from ".mission::$table." where uid=$uid and mid=$mid";
		mysql::run($command);
	}
	
	public static function getCurrentTime()
	{
		date_default_timezone_set('Asia/Shanghai');
		$d = date('Y-m-d');
		$t = date('g:i:s');
		$time_now = $d." ".$t;
		
		return $time_now;
	}
	
	/////////////////////////////////////////////////////////////////
	private $pid;
	
	private $uid;
	private $mid;
	private $canget;
	private $isgot;
	private $gottime;
	private $reach;
	private $complete;
	private $expired;
	
	private $detail;
	
	public function __set($name, $value) 
	{
		if ( isset( $this->$name ) )
		{       
			if($name!='pid' && $name!='detail')
			{
				$command = "update ".mission::$table." set $name='$value' where pid={$this->pid} ";
				mysql::run($command);
			}	
		}
		
		$this->$name = $value;
	}
	public function __get($name) 
	{
		if( isset( $this->$name ) ) 
		{    
			if($name!='pid' && $name!='detail')
			{
				$command = "select * from ".mission::$table." where pid={$this->pid}";
				$result =  mysql::run($command);
				$vals=mysql_fetch_array($result);
				$this->$name = $vals[$name];
			}
			
			return $this->$name;
			
		} else {
			trigger_error( $name . ' variables undefined',  E_USER_NOTICE );
		}
	}
	/////////////////////////////////////////
	public function mission($pid)
	{
		$this->pid = $pid;
		
		$command = "select * from ".mission::$table." where pid=$pid";
		$result =  mysql::run($command);
		$data=mysql_fetch_array($result);
		
		$this->uid = $data["uid"];
		$this->mid = $data["mid"];
		$this->canget = $data["canget"];
		$this->isgot = $data["isgot"];
		$this->gottime = $data["gottime"];
		$this->reach = $data["reach"];
		$this->complete = $data["complete"];
		$this->expired = $data["expired"];
		
		$this->detail = new missionDetail($this->mid);
	}
	
	/**
	 *检查任务是否过期
	 * @return 0：正常; 1：过期
	 */	
	public function checkValid()
	{
		$valid = $this->detail->valid;
		
		if($valid==0)return 0;
		else{
			$timeNow =  mission::getCurrentTime();
			$s = strtotime($this->gottime)-strtotime($timeNow);
			
			if($s>$valid*3600)return 1;
			else return 0;
		};
	}
	/**
	 * 检查是否已达到可接要求
	 * @return 1：可接; 0：不可接
	 */	
	public function checkAcquire($user)
	{
		$acquireRequire = $this->detail->acquireRequire;
		
		$itemList = $user->getItems();
		
		$gotAcquire = 1;
		
		/////////////////////////////
		if($user->level<$acquireRequire['level'])$gotAcquire = 0;
		if($user->money<$acquireRequire['money'])$gotAcquire = 0;
		
		/////////////////////////////
		foreach($itemList as $item)
		{
			foreach($acquireRequire['item'] as $acItem)
			{
				if($item['id']==$acItem['id'] && $item['inc']==$acItem['inc'])$acItem['num']-=1;
			}
		}
		foreach($acquireRequire['item'] as $acItem)
		{
			if($acItem['num']>0)
			{
				$gotAcquire = 0;
				break;
			}
		}
		
		/////////////////////////////
		$this->canget = $gotAcquire;
		return $gotAcquire;
	}
	
	/**
	 * 检查是否已达到完成任务要求
	 * @return 0:没达到; 1:达到
	 */	
	public function checkReach($user)
	{
		$completeRequire = $this->detail->completeRequire;
		$itemList = $user->getItems();
		
		$reach = 1;
		
		foreach($itemList as $item)
		{
			foreach($completeRequire['item'] as $coItem)
			{
				if($item['id']==$coItem['id'] && $item['inc']==$coItem['inc'])$coItem['num']-=1;
			}
		}
		foreach($acquireRequire['item'] as $coItem)
		{
			if($coItem['num']>0)
			{
				$reach = 0;
				break;
			}
		}
		
		/////////////////////////////
		$this->reach = $reach;
		return $reach;
	}
	
}

?>

